#!/bin/sh

#  If the daemon is already executing in the background, find it's process and terminate it.
#  We need to do this BEFORE attempting to unload the driver because the driver won't unload
#  if the daemon is holding a reference to it.

r=`ps ax |grep SATARaid5Daemon | grep -v grep | grep -v $$ |grep -v $PPID | grep -v stop | grep -v start | awk '{print $1}'`
if [ "${r}" != "" ] ; then
    sudo /bin/kill -9 $r
fi

#  If an old version of the 3132 driver is loaded,
#  then remove it from the system.
#  The old version may be named Si3132r5 or SiliconImage3132r5

#a=`sudo kextstat | grep Si3132r5.kext`
#if [ "$a" != "" ] ; then
#    sudo kextunload Si3132r5.kext
#fi

a=`sudo kextstat | grep com.SiliconImage.driver.Si3132r5`
if [ "$a" != "" ] ; then
    sudo kextunload -b com.SiliconImage.driver.Si3132r5
fi

#  If an older version of the driver using the old name (si3132r5) 
#  is present in the extensions directory, remove it before installing 
#  the new version.

if [ -d /System/Library/Extensions/Si3132r5.kext ] ; then
    sudo rm -rf /System/Library/Extensions/Si3132r5.kext
fi
